package com.gherasoft.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import com.gherasoft.model.CheckListItem;
import com.gherasoft.model.CheckListModel;
import com.gherasoft.view.R;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * User: sckomoroh
 * Date: 3/12/13
 * Time: 9:49 PM
 */
public class EditCheckListAdapter extends BaseAdapter
{
    private CheckListModel model;
    private Context context;

    public EditCheckListAdapter(Context context)
    {
        this.context = context;
        this.model = new CheckListModel(this);
    }

    public EditCheckListAdapter(Context context, CheckListModel model)
    {
        this.context = context;
        this.model = model;
    }

    public CheckListModel getModel()
    {
        return this.model;
    }

    public int addItem(String item)
    {
        int index = this.model.addItem(item);
        notifyDataSetChanged();

        return index;
    }

    public void removeItem(int index)
    {
        this.model.removeItem(index);
        notifyDataSetChanged();
    }

    public void setItem(int index, String item)
    {
        this.model.setItem(index, item);
        notifyDataSetChanged();
    }

    @Override
    public int getCount()
    {
        return this.model.getCount();
    }

    @Override
    public Object getItem(int index)
    {
        return this.model.getItem(index);
    }

    @Override
    public long getItemId(int index)
    {
        return index;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup)
    {
        final int index = i;

        LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.edit_check_list_item, null);

        TextView textView = (TextView) view.findViewById(R.id.editItemTextView);
        textView.setText(this.model.getItemTitle(index));

        ImageButton removeButton = (ImageButton) view.findViewById(R.id.editItemRemoveButton);
        removeButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                CheckListItem item = (CheckListItem) EditCheckListAdapter.this.getItem(index);
                String itemName = item.getTitle();
                String dialogMessage = String.format(
                        "Do you want to remove '%s' item",
                        itemName);

                AlertDialog.Builder alt_bld = new AlertDialog.Builder(EditCheckListAdapter.this.context);
                alt_bld.setMessage(dialogMessage)
                        .setCancelable(false)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener()
                        {
                            public void onClick(DialogInterface dialog, int id)
                            {
                                EditCheckListAdapter.this.removeItem(index);
                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener()
                        {
                            public void onClick(DialogInterface dialog, int id)
                            {
                                dialog.cancel();
                            }
                        });

                AlertDialog alert = alt_bld.create();
                // Title for AlertDialog
                alert.setTitle("Remove item");
                // Icon for AlertDialog
                //alert.setIcon(R.drawable.ic_launcher);
                alert.show();
            }
        });

        return view;
    }
}
